//
//  ViewController.m
//  class
//
//  Created by Luck on 2017/9/9.
//  Copyright © 2017年 yinjiao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    UITableView *_tableView;
    NSMutableArray *data;
    NSArray * backup;
    
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"我的通讯录";
    data =[NSMutableArray new];
    backup = @[].copy;
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"tongxulu" ofType:@"plist"];
    NSMutableArray *datad = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    data = datad.mutableCopy;
    backup = datad.copy;
    
    NSLog(@"%@", datad);//直接打印数据。
    
    UISearchBar *bar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    bar.delegate =self;
    self.navigationItem.titleView =bar;
    
    
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    NSLog(@"%@",searchText);
    if (searchText.length == 0) {
        data = backup.mutableCopy;
    }else {
        NSMutableArray *res = [NSMutableArray new];
        for (NSDictionary *tel in backup) {
            if ([tel[@"tel"] containsString:searchText]) {
                [res addObject:tel];
            }
        }
        data = res.mutableCopy;
    }
    [_tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    //cell重用
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    
    cell.textLabel.text =data[indexPath.row][@"name"];
    cell.detailTextLabel.text=data[indexPath.row][@"tel"];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    NSString *tel = data[indexPath.row];
    //    NSArray *tmp =[tel componentsSeparatedByString:@","];
    //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",tmp.lastObject]]];
#pragma mark 拨打电话
    UIAlertController * sheetAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *telAction = [UIAlertAction actionWithTitle:@"拨打电话" style:0 handler:^(UIAlertAction * _Nonnull action) {
        NSString *phoneNum =[[NSString alloc]init];
        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNum];
        UIWebView *callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
    }];
    UIAlertAction *smsAction = [UIAlertAction actionWithTitle:@"发送短信" style:0 handler:^(UIAlertAction * _Nonnull action) {
        NSString *phoneNum =[[NSString alloc]init];
        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"sms:%@",phoneNum];
        UIWebView *callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
    }];
    
    UIAlertAction *qxAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        nil;
    }];
    
    [sheetAlert addAction:telAction];
    [sheetAlert addAction:smsAction];
    [sheetAlert addAction:qxAction];
    [self presentViewController:sheetAlert animated:YES completion:nil];
    
    
    
    
}
//- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewRowAction *no1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        [data removeObjectAtIndex:indexPath.row];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//        /*风格*/
//    }];
//
//    UITableViewRowAction *no2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"拨号" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//
//        [self call:data[indexPath.row][@"tel"]];
//        NSLog(@"%@",data[indexPath.row][@"tel"]);
//    }];
//    no2.backgroundColor = [UIColor blueColor];
//
//    return @[no1,no2];
//}



//- (void)call:(NSString *)phoneNum
//{
//    if (!phoneNum) {
//        phoneNum = @"10086";
//    }
//    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNum];
//    UIWebView *callWebview = [[UIWebView alloc] init];
//    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
//    [self.view addSubview:callWebview];
//
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
